# -*- coding: UTF-8 -*-
# 2023/11/30 20:49
from ttkbootstrap import Checkbutton, BooleanVar
from ttkbootstrap.scrolled import ScrolledFrame

from tools.get_configer import my_configer
from tools.get_logger import get_logger
from tools.img_tool import set_pil_img

configer = my_configer()
loger = get_logger()


class phone_top_frame(ScrolledFrame):
    def __init__(self, class_id_cover_url_name_save_path_list, line_num, **kwargs):
        super().__init__(**kwargs)
        self.line_num = line_num
        self.class_id_cover_url_name_save_path_list = class_id_cover_url_name_save_path_list

        self.download_page_btn_list = list()  # 生成的所有按钮
        self.btn_variable_list = list()

        self.add_img_class()

    def add_img_class(self):
        """
        以按钮图片形式添加图片到ScrolledFrame中
        """
        row = 0
        for column in range(len(self.class_id_cover_url_name_save_path_list)):
            class_id_cover_url_name_save_path = self.class_id_cover_url_name_save_path_list[column]
            class_id = class_id_cover_url_name_save_path[0]
            class_name = class_id_cover_url_name_save_path[2]
            cover_save_path = class_id_cover_url_name_save_path[3]

            # 创建组件并给组件设置对应封面
            btn_variable = BooleanVar()
            check_btn_image = set_pil_img(cover_save_path, (256, 192))
            check_btn = Checkbutton(self, variable=btn_variable, text=class_name, image=check_btn_image, compound='bottom')
            check_btn.image = check_btn_image

            # 绑定class_id和name
            setattr(check_btn, "class_id", class_id)
            setattr(check_btn, "class_name", class_name)

            setattr(btn_variable, "class_id", class_id)
            setattr(btn_variable, "class_name", class_name)

            # 加入列表
            self.download_page_btn_list.append(check_btn)
            self.btn_variable_list.append(btn_variable)

            # 布局和拉伸策略
            if column % self.line_num == 0 and column != 0:
                row += 1
            check_btn.grid(row=row, column=column % self.line_num, ipadx=10, ipady=10)
            self.master.grid_rowconfigure(row, weight=1)
            self.master.grid_columnconfigure(column % self.line_num, weight=1)

            loger.info(f"批量生成多选按钮预览,按钮绑定信息:{class_id_cover_url_name_save_path}")

    def get_select_btn(self):
        """
        获取当前所有已勾选的按钮列表
        """

        download_id_name_list = list()
        for btn_variable in self.btn_variable_list:
            button_select_state = btn_variable.get()
            class_name = getattr(btn_variable, "class_name")
            class_id = getattr(btn_variable, "class_id")

            if button_select_state:
                download_id_name_list.append([class_id, class_name])
                loger.info(f"提交选择分类--{class_name}, class_id:{class_id}")

        return download_id_name_list

    def get_all_btn(self):
        """
        获取当前所有已勾选的按钮列表
        """
        download_id_name_list = list()
        for btn_variable in self.btn_variable_list:
            class_name = getattr(btn_variable, "class_name")
            class_id = getattr(btn_variable, "class_id")
            download_id_name_list.append([class_id, class_name])
            loger.info(f"提交全部分类--{class_name}, class_id:{class_id}")

        return download_id_name_list
